package com.hospital.hospitalsystem.modules.user.controller;

import com.hospital.hospitalsystem.base.BaseResponse;
import com.hospital.hospitalsystem.base.PageData;
import com.hospital.hospitalsystem.config.TokenVerify;
import com.hospital.hospitalsystem.modules.user.biz.OrderBiz;
import com.hospital.hospitalsystem.modules.user.dto.*;
import com.hospital.hospitalsystem.utils.LoginUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author qiancheng@cqxyy.net
 * @date 2023/4/19
 */
@RestController
@RequestMapping("/order")
@Api(tags = "订单管理")
@Validated
public class OrderController {

    @Autowired
    private OrderBiz orderBiz;

    @Autowired
    private LoginUtils loginUtils;

    @ApiOperation(value = "结算界面获取商品列表")
    @RequestMapping(path = "/before-placeOrder",method = RequestMethod.POST)
    @TokenVerify(type = "user,medicine")
    public BaseResponse beforePlaceOrder(@RequestBody BeforePlaceOrderReq req){
        req.setUserUuid(loginUtils.getUser().getUserUuid());
        List<BeforePlaceOrderRsp> list = orderBiz.beforePlaceOrder(req);
        return BaseResponse.success(list);
    };

    @ApiOperation(value = "购买商品")
    @RequestMapping(path = "/buy-medical",method = RequestMethod.POST)
    @TokenVerify(type = "user")
    public BaseResponse buyMedical(@RequestBody BuyOrderReq req){
        req.setUserUuid(loginUtils.getUser().getUserUuid());
        orderBiz.buyMedical(req);
        return BaseResponse.success();
    };

    @ApiOperation(value = "查看订单")
    @RequestMapping(path = "/list",method = RequestMethod.POST)
    @TokenVerify(type = "user,medicine")
    public BaseResponse<PageData<SearchOrderRsp>> medicalList(@RequestBody SearchOrderListReq req){
        if (loginUtils.getUser().getRoles().equals("user")) {
            req.setUserUuid(loginUtils.getUser().getUserUuid());
        }
        PageData<SearchOrderRsp> result = orderBiz.medicalList(req);
        return BaseResponse.success(result);
    };

    @ApiOperation(value = "取消订单")
    @RequestMapping(path = "/delete",method = RequestMethod.POST)
    @TokenVerify(type = "user")
    public BaseResponse medicalDelete(@RequestBody OrderMedicalReq req){
        req.setUserUuid(loginUtils.getUser().getUserUuid());
        orderBiz.medicalDelete(req);
        return BaseResponse.success();
    };

    @ApiOperation(value = "确认收货")
    @RequestMapping(path = "/success",method = RequestMethod.POST)
    @TokenVerify(type = "user")
    public BaseResponse medicalSuccess(@RequestBody OrderMedicalReq req){
        req.setUserUuid(loginUtils.getUser().getUserUuid());
        orderBiz.medicalSuccess(req);
        return BaseResponse.success();
    };

    @ApiOperation(value = "填写快递单号并出库")
    @RequestMapping(path = "/send",method = RequestMethod.POST)
    @TokenVerify(type = "medicine")
    public BaseResponse medicalSend(@RequestBody OrderMedicalReq req){
        req.setUserUuid(loginUtils.getUser().getUserUuid());
        orderBiz.medicalSend(req);
        return BaseResponse.success();
    };

    @ApiOperation(value = "获取快递公司")
    @RequestMapping(path = "/express-company",method = RequestMethod.GET)
    @TokenVerify(type = "medicine")
    public BaseResponse expressCompany(){
        List<ExpressCompanyRsp> list = orderBiz.expressCompany();
        return BaseResponse.success(list);
    };

    @ApiOperation(value = "根据订单号查询详细信息")
    @RequestMapping(path = "/get-detail",method = RequestMethod.POST)
    @TokenVerify(type = "medicine,user")
    public BaseResponse getDetail(@RequestBody OrderMedicalReq req){
        if (loginUtils.getUser().getRoles().equals("user")) {
            req.setUserUuid(loginUtils.getUser().getUserUuid());
        }
        OrderDetailRsp rsp = orderBiz.getDetail(req);
        return BaseResponse.success(rsp);
    };

    @ApiOperation(value = "抓药师根据订单号和用户uuid撤回订单")
    @RequestMapping(path = "/cancel",method = RequestMethod.POST)
    @TokenVerify(type = "medicine")
    public BaseResponse cancel(@RequestBody OrderCancelReq req){
        orderBiz.cancel(req);
        return BaseResponse.success();
    };
}
